import React from 'react'
import { connect } from 'react-redux'
import BookRecommendModalUI from './BookRecommendModalUI'
import PubSub from 'pubsub-js'
import { FIND_FRIENDS } from '../constant'
import { setAddFriend } from '../../action/action'
import { getUInfo } from '../../../session/session'

class BookRecommendModal extends React.PureComponent {
  constructor (props) {
    super(props)
    this.state = {
      visible: false
    }
  }

  onCancel = () => {
    this.setState({
      visible: false
    })
  }
  addFriends = (info) => {
    // 加好友
    const uInfo = JSON.parse(getUInfo())
    this.props.dispatch(setAddFriend(uInfo.readerId, info.readerId))
  }
  showModal = () => {
    this.setState({
      visible: true
    })
  }

  componentDidMount () {
    //订阅的事件
    this.pubsub_token = []
    this.pubsub_token.push(PubSub.subscribe(FIND_FRIENDS, this.showModal))
  }

  render () {
    const { visible } = this.state
    const { onCancel, addFriends } = this
    const { tableData } = this.props
    return (
      <BookRecommendModalUI
        visible={visible}
        onCancel={onCancel}
        addFriends={addFriends}
        tableData={tableData.toJS()}
      />
    )
  }
}

const mapStateToProps = (state) => {
  return {
    tableData: state.get('getBookRecommend')
  }
}
export default connect(mapStateToProps)(BookRecommendModal)